package mypkg;

public class Main {
    public static void main(String[] args) {
        String S="B BB BBBA BBBB BBBBBA AAAAAAAAAAAA AAABBB";
        String[] M=S.split(" ");
        for (int j=0;j<M.length;j++) for (int i=1;i<M.length-j;i++) if (less(M[i],M[i-1])) {
           String t=M[i-1];
           M[i-1]=M[i];
           M[i]=t;
        }
        String R="";
        for (int i=0;i<M.length;i++) {
            if (i!=0) R+=" ";
            R+=M[i];
        }
        System.out.println(R);
    }
    
    public static boolean less(String S1, String S2) {
        int m1=0, m2=0;
        for (int i=0;i<S1.length();i++) if (isS(S1.toUpperCase().charAt(i))) m1++;
        for (int i=0;i<S2.length();i++) if (isS(S2.toUpperCase().charAt(i))) m2++;
        return m1*S2.length()<m2*S1.length();
    }

    public static boolean isS(char c) {
        switch (c) {
            case 'B': case 'C': case 'D': case 'F': case 'G': case 'H':
            case 'J': case 'K': case 'L': case 'M': case 'N': case 'P':
            case 'Q': case 'R': case 'S': case 'T': case 'V': case 'W':
            case 'X': case 'Z':
                return true;
        }
        return false;
    }
}
